﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myAutoExcuteJobService
{
  public   class Ado
    {
        string SqlServerDBConstring = GV.Blog;// "Data Source=.;Initial Catalog=Edoc2.Form;Integrated Security=True";
      public DataSet query(string sqlstr)
      {
         DataSet ds = new DataSet();
           try
           {
               SqlConnection con = new SqlConnection(SqlServerDBConstring);
               con.Open();
               //2    创建Command语句
               SqlCommand cmd = new SqlCommand(sqlstr, con);
               //   执行命令（执行命令之前，必须打开数据库）
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               
               da.Fill(ds);
               con = new SqlConnection(SqlServerDBConstring);
               //DataTable dt = ds.Tables[0];
               //DataRowCollection rows = dt.Rows;
               //DataRow dr = rows[0];
               //string str = (string)dr[0];
               con.Close();
           }
           catch (Exception e) 
           {
               new Tools.Dbaction().writedblog(0, DateTime.Now.ToString(), "查询失败", e.ToString());
           }
           return ds;
       }
       public  int insert(string sqlstr) 
       {
           int count=0;
         try{
           //1、创建数据库连接对象，并编写连接字符串，注意连接字符串不要写错
           SqlConnection conn = new SqlConnection(SqlServerDBConstring);

           //2、创建数据库操作对象，创建过程是与刚创建的连接对象匹配起来
           SqlCommand cmd = conn.CreateCommand();

           //3、编写操作语句 TSQL语句
           cmd.CommandText = sqlstr;

           //4、数据库连接打开，准备执行操作
           conn.Open();

           //5、执行操作，并记录受影响的行数
            count = cmd.ExecuteNonQuery();

           //6、关闭数据库连接**********
           conn.Close();
               }
           catch (Exception e) 
           {
               new Tools.fileAtion().writelog(e.ToString() + sqlstr, "mylog.txt");        //  new Tools.Dbaction().writedblog(System.DateTime.Now.ToString() + e.ToString() + "**" + "**********");     
           }
           return count;
       }

       public  int delete(string sqlstr) 
       {
           int number = 0;
           try
           {
               //1、创建数据库连接类
               string sql = SqlServerDBConstring; //编写连接字符串
               SqlConnection conn = new SqlConnection(sql);

               //2、创建数据库操作类
               SqlCommand cmd = conn.CreateCommand();
               //3输入要操作删除的信息
               cmd.CommandText = sqlstr;
               //4打开数据库连接
               conn.Open();
               //5引进一个变量来记录受影响条数
                number = cmd.ExecuteNonQuery();
               //6关闭数据库
               conn.Close();
           }
           catch (Exception e)
           {
               new Tools.Dbaction().writedblog(0, DateTime.Now.ToString(), "删除失败", e.ToString());
               
           }
           return number;
       }

        public  int updata(string sqlstr) 
       {
           int number = 0;
            try{
           //1、创建数据库连接类
           string sql = SqlServerDBConstring; //编写连接字符串
           SqlConnection conn = new SqlConnection(sql);

           //2、创建数据库操作类
           SqlCommand cmd = conn.CreateCommand();
           //3输入要操作genxin的信息
           cmd.CommandText = sqlstr;
           //4打开数据库连接
           conn.Open();
           //5引进一个变量来记录受影响条数
           number = cmd.ExecuteNonQuery();
           //6关闭数据库
           conn.Close();
            }
            catch (Exception e)
            {
                new Tools.Dbaction().writedblog(0, DateTime.Now.ToString(), "更新失败", e.ToString());
                 }
           return number;
       
       
       
       
       
       }





         }
}
